@file:Suppress("UNUSED_PARAMETER")

package cn.jimmie.learn.viewArt.fragment

import android.os.CountDownTimer
import android.view.View
import android.widget.Button
import android.widget.Toast
import cn.jimmie.learn.viewArt.R
import cn.jimmie.learn.viewArt.ball.AssistBall
import kotlinx.android.synthetic.main.fragment_ball.view.*
import java.util.*

/**
 * function :
 *
 * <p> Create Time:  2019年01月16日 12:55 </p>
 * <p> @author Jimmie.Qian  </p>
 */
class AssistBallFragment : BaseFragment() {
    private val contents = arrayOf(
        "浮窗可关闭，5秒未操作则自动消失，拖动浮球的话也关闭浮白提示，悬浮球浮窗提示类型如下!",
        "新功能引导，详细规则查看【悬浮球 引导】，新功能引导浮窗优先级最高，消失后在出现下列其他提示，样式如下!",
        "打开游戏时，收到新消息时，提示如下，点击【3】直接进入消息列表!",
        "当同时有多类型的消息时（不包括新功能引导）!"
    )

    private val titles = arrayOf(null, "打扰游戏,长按锁定吧!", "自定义title~", "悬浮球红点提示")
    private val random = Random()
    private var pingBtn: Button? = null
    private var dotBtn: Button? = null
    private var showBtn: Button? = null
    private var ball: AssistBall? = null
    private var hasRed = false

    override fun initView(root: View) {
        pingBtn = root.ping
        dotBtn = root.red_dot
        showBtn = root.show

        root.ping.setOnClickListener { onPingClick(it) }
        root.red_dot.setOnClickListener { onRedDotClick(it) }
        root.message.setOnClickListener { onSendMessageClick(it) }
        root.show.setOnClickListener { onShowClick(it) }
        root.state.setOnClickListener { onCheckClick(it) }

    }

    override fun layoutId(): Int = R.layout.fragment_ball

    private fun onRedDotClick(view: View) {
        if (ball == null) return
        hasRed = !hasRed
        if (hasRed) {
            ball!!.setRedDot(true)
            dotBtn!!.text = "隐藏小红点"
        } else {
            ball!!.setRedDot(false)
            dotBtn!!.text = "显示小红点"
        }
    }

    private fun onSendMessageClick(view: View) {
        if (ball == null) return
        val title = titles[random.nextInt(titles.size)]
        val content = contents[random.nextInt(contents.size)]
        val hasLink = random.nextBoolean()

        ball!!.addMessage(AssistBall.MessageBuilder()
            .title(title)
            .content(content)
            .enableMessageClick(hasLink)
            .enableMulti(false)
            .create())
    }

    private var isPing: Boolean = false

    private fun onPingClick(view: View) {
        if (ball == null) return
        isPing = !isPing
        if (isPing) {
            timer.cancel()
            timer.start()
            pingBtn!!.text = "关闭随机ping显示"
        } else {
            ball!!.updatePing(0)
            timer.cancel()
            pingBtn!!.text = "开启随机ping显示"
        }
    }

    private val timer = object : CountDownTimer((1000 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            ball!!.updatePing(random.nextInt(10000))
        }

        override fun onFinish() {

        }
    }
    private var isShow: Boolean = false

    private fun onCheckClick(view: View) {
        if (ball == null)
            Toast.makeText(context, "浮球显示状态:false", Toast.LENGTH_SHORT).show()
        else
            Toast.makeText(context, "浮球显示状态:" + ball!!.isAttached(), Toast.LENGTH_SHORT).show()
    }

    private fun createBall(style: Int) {

        ball = AssistBall.creator(activity)
            .setStyle(style)
            .setPosition(style)
            .setPingEdges(6000, 9000)
            .create()
    }

    private fun onShowClick(view: View) {
        isShow = !isShow
        if (isShow) {
            createBall(random.nextInt(4))
            ball!!.attacheToWindow()
            showBtn!!.text = "手动关闭悬浮球"
        } else {
            ball!!.detachFromWindow()
            showBtn!!.text = "手动开启悬浮球"
        }
    }
}